package com.xnew.memInfoChgHist.service.impl;

import cn.hutool.core.date.DateUtil;
import com.xnew.bean.common.SysUserInfo;
import com.xnew.entity.memberArchives.MemInfoChgHist;
import com.xnew.memInfoChgHist.mapper.MemInfoChgHsitMapper;
import com.xnew.memInfoChgHist.service.MemInfoChgHsitService;
import com.xnew.utils.IdUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author zhozh
 * @date 2022/7/16 16:33
 * @schema: xxxxx
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MemInfoChgHsitServiceImpl implements MemInfoChgHsitService {

    @Resource
    private MemInfoChgHsitMapper memInfoChgHsitMapper;

    /**
    * @Author: Zhozh
    * @Description: 获取变更字段的变更前和变更后的数据内容
    * @DateTime: 2022/7/16
    * @Params: oldInfo--变更前的对象 newInfo 变更后的对象 ignoreArr 忽略的字段
    * @Return
    */
    @Override
    public Map<String, List<Object>> compareFields(Object oldInfo, Object newInfo, String[] ignoreArr, SysUserInfo sysUserInfo,String memberInfoNo) {
        try{
            Map<String, List<Object>> map = new HashMap<String, List<Object>>();
            List<String> ignoreList = null;
            if(ignoreArr != null && ignoreArr.length > 0){
                // array转化为list
                ignoreList = Arrays.asList(ignoreArr);
            }
            if (oldInfo.getClass() == newInfo.getClass()) {// 只有两个对象都是同一类型的才有可比性
                Class clazz = oldInfo.getClass();
                // 获取object的属性描述
                PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz,
                        Object.class).getPropertyDescriptors();
                for (PropertyDescriptor pd : pds) {// 这里就是所有的属性了
                    String name = pd.getName();// 属性名
                    if(ignoreList != null && ignoreList.contains(name)){// 如果当前属性选择忽略比较，跳到下一次循环
                        continue;
                    }
                    Method readMethod = pd.getReadMethod();// get方法
                    // 在obj1上调用get方法等同于获得obj1的属性值
                    Object o1 = readMethod.invoke(oldInfo);
                    // 在obj2上调用get方法等同于获得obj2的属性值
                    Object o2 = readMethod.invoke(newInfo);
                    if(o1 instanceof Timestamp){
                        o1 = new Date(((Timestamp) o1).getTime());
                    }
                    if(o2 instanceof Timestamp){
                        o2 = new Date(((Timestamp) o2).getTime());
                    }
                    if(o1 == null && o2 == null){
                        continue;
                    }else if(o1 == null && o2 != null){
                        List<Object> list = new ArrayList<Object>();
                        list.add(o1);
                        list.add(o2);
                        map.put(name, list);
                        continue;
                    }
                    if (!o1.equals(o2)) {// 比较这两个值是否相等,不等就可以放入map了
                        List<Object> list = new ArrayList<Object>();
                        list.add(o1);
                        list.add(o2);
                        map.put(name, list);
                    }
                }
            }
            System.out.println( "===============" +map.toString());
            for (Map.Entry<String, List<Object>> stringListEntry : map.entrySet()) {
                //获取修改的字段名
                String column = stringListEntry.getKey();
                List<Object> list = stringListEntry.getValue();
                System.out.println("list = " + list);
                //获取变更的信息前后数据内容
                MemInfoChgHist memInfoChgHist = new MemInfoChgHist();
                memInfoChgHist.setHistChgInfoNo(IdUtils.getIncreaseIdByNanoTime());
                memInfoChgHist.setChgColName(column);
                memInfoChgHist.setChgAttrName(column.getClass().getName());
                memInfoChgHist.setChgBefrData(ObjectUtils.isEmpty(stringListEntry.getValue().get(0))?"":stringListEntry.getValue().get(0).toString());
                memInfoChgHist.setChgAftrAdta(ObjectUtils.isEmpty(stringListEntry.getValue().get(1))?"":stringListEntry.getValue().get(1).toString());
                memInfoChgHist.setInfoInputTime(DateUtil.now());
                memInfoChgHist.setInfoChgPsnNo(sysUserInfo.getUserInfoNo());
                memInfoChgHist.setIsDelChg("0");
                memInfoChgHist.setIsAttachType("0");
                memInfoChgHist.setOrgUserNo(sysUserInfo.getOrgUserNo());
                memInfoChgHist.setMemInfoNo(memberInfoNo);
                memInfoChgHsitMapper.insert(memInfoChgHist);
            }
            return map;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }


}
